﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CourseManager.WebUI.ViewModels;
using LDAP_Manager;
using System.Web.Security;

namespace CourseManager.WebUI.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(LoginModel pCredentials)
        {
            if (ModelState.IsValid)
            {
                LDAPManager Manager = new LDAPManager();
                ADUser CurrentUser = Manager.LogUser(pCredentials.UserName, pCredentials.Password);

                if (CurrentUser != null)
                {
                    FormsAuthentication.SetAuthCookie(CurrentUser.FirstName + " " + CurrentUser.LastName, pCredentials.SavePassword);
                    return RedirectToAction("GetCourse", "Course", CurrentUser);
                }
                else
                {
                    ModelState.AddModelError("Username or Password incorrect", "The username or password provided was incorrect");
                    return View();
                }
            }
            else
            {
                return View();
            }
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index","Login");
        }

    }
}
